// department.js
var wxCharts = require('../../common/wxcharts-min.js');
import SPECIAL from '../../API/SPECIAL';
import NumberAnimate from "../../lib/NumberAnimate";
var app = getApp()
var columnChart = null;
var speChart = null;
var pieChart = null;
import APIURL from '../../const/APIURL'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showpieChart: false,
    currentspec: {},
    specials: [],
    wholeschool: {},
    showTopTips: false,
    errorMsg: '',
    percents: [],
    specnames: [],
    index: 0,
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var index = e.detail.value;
    var that = this;
    this.setData({
      index: index,
      currentspec: that.data.specials[index]
    })
    wx.setStorageSync("currentspecid", that.data.specials[index].spe_Id)
    that.RefreshspeChart();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  
    wx.setNavigationBarTitle({
      title: '专业报到情况'
    })
    this.hud = app.hudClass(this).hud;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.role != 'admin') {
      wx.showModal({
        title: '提示',
        content: '你好，游客不可以访问当前页面，点击返回！',
        showCancel: false,
        confirmText: '知道了',
        confirmColor: '',
        success: function () {
          wx.switchTab({
            url: '/pages/department/department',
          })
        },
      })
      return;
    }
    this.RefreshData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.RefreshData();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {


  },
  prepareData: function () {
    var that = this;
    var d = that.data.specials;
    var specnames = [];
    var spechavenums = [];
    var specregnums = [];
    var specpercent = [];
    for (var i = 0; i < d.length; i++) {
      specnames.push(d[i].spe_Name);
      spechavenums.push(d[i].spec_Havenum);
      specregnums.push(d[i].spec_Regnum);
      specpercent.push((d[i].spec_Percent * 100).toFixed(1));
    }

    var selectid = 1;
    var id = wx.getStorageSync("currentspecid");
    if (id && id != undefined && id != null && id >= 0) {
      selectid = id;
    }
    else
    {
      wx.setStorageSync("currentspecid",that.data.specials[0].spe_Id)
    }
    
    this.setData({
      percents: specpercent,
      specnames: specnames,
      specials: d,
      index: that.getOrder(selectid)
    });

    
  },


  RefreshspeChart: function () {
    var selectid = 1;
    var id = wx.getStorageSync("currentspecid");

    if (id != undefined && id != null && id >= 0) {
      selectid = id;
    }
    console.log(selectid)
    var d = this.getSpecial(selectid);
    console.log(d);
    var that = this;
    speChart = new wxCharts({
      canvasId: 'specCanvas',
      type: 'column',
      categories: ['实到', '应到'],
      series: [{
        name: '报到人数',
        data: [d.spec_Havenum, d.spec_Regnum],
        format: function (val, name) {
          return val + '人';
        }
      }
      ],
      yAxis: {
        format: function (val) {
          return val + '人';
        },
        min: (d.spec_Havenum * 0.1).toFixed(0)

      },
      xAxis: {
        fontColor: '#7cb5ec',
      },
      width: 350,
      height: 300
    });

    if (d.spe_boynum == 0 && d.spe_girlnum == 0) {
      that.setData({
        showpieChart: false
      })
      return;
    }
    pieChart = new wxCharts({
      animation: true,
      canvasId: 'pieCanvas',
      type: 'ring',
      title: {
        name: "男女生比例"
      },
      series: [{
        name: '男生',
        data: d.spe_boynum,
      }, {
        name: '女生',
        data: d.spe_girlnum,
      }],
      width: 350,
      height: 300,
      dataLabel: true,
      dataPointShape: true,
      disablePieStroke: true
    });
    that.setData({
      showpieChart: true
    })
  },
  RefreshData: function () {    
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    that.setData({
      specials: [{}],
      havenum: 0,
      regnum: 0,
    })
    SPECIAL.GETSPECIALS().then(function (d) {
      if (d.responseResult != 1) {
        
        wx.showToast({
          title: d.msg ? '' : d.msg,
          icon: 'loading',
          duration: 1000,
          mask: true
        })
        that.setData({
          showTopTips: true,
          errorMsg: d.msg
        })

        setTimeout(function () {
          that.setData({
            showTopTips: false,
            errorMsg: ''
          })
        }, 2000)
      }
      else {
        wx.showToast({
          title: "加载成功",
        })
        //console.log(d)
        that.setData({
          specials: d.data
        })
        that.prepareData();
        that.RefreshspeChart();
      }
  
    }, function (d) {
      wx.hideLoading()
    })
  },

  getSpecial: function (speid) {
    var that = this;
    var specs = that.data.specials;
    var spec;
    for (var i = 0; i < specs.length; i++) {
      if (specs[i].spe_Id == speid) {
        spec = specs[i]

        console.log(spec)
 
        if (spec.spec_Regnum == spec.spec_Havenum)
        {
          spec.Percent=100.0; 
        }
        else{
          spec.Percent = ((spec.spec_Percent) * 100).toFixed(1);
        }
        let n1 = new NumberAnimate({
          from: spec.spec_Havenum,
          speed: 1000,
          refreshTime: 100,
          decimals: 0,
          onUpdate: () => {
            spec.spec_Havenum = n1.tempValue;
            this.setData({
              currentspec: spec
            });
          },
          onComplete: () => {
            console.log('完成')
          }
        });
        that.setData(
          {
            currentspec: spec
          }
        )
      }
     
    }
    return spec;
  },
  getOrder: function (speid) {
    var that = this;
    var specs = that.data.specials;
    for (var i = 0; i < specs.length; i++) {
      if (specs[i].spe_Id == speid) {
        return i;
      }
    }
    return 0;
  },
})
